package com.briup.srms.sms.web.controller;

import com.briup.srms.exam.bean.extend.StudentExamExtend;
import com.briup.srms.sms.bean.EmployeeEvent;
import com.briup.srms.sms.bean.extend.StudentExtend;
import com.briup.srms.sms.service.IStudentPrimaryService;
import com.briup.srms.sms.service.IStudentService;
import com.jacky.base.utils.ExcelUtils;
import com.jacky.base.utils.Message;
import com.jacky.base.utils.MessageUtil;
import com.jacky.base.utils.PageVM;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: sms
 * @description: 隐私接口
 * @author: charles
 * @create: 2022-02-22 10:03
 **/
@Api(description = "学生非隐私数据相关接口")
@Validated
@RestController
@RequestMapping("/student/primary")
public class StudentPrimaryController {
    @Autowired
    private IStudentService studentService;
    @Autowired
    private IStudentPrimaryService studentPrimaryService;


    @ApiOperation(value = "分页多条件获取学生信息")
    @GetMapping("pageQuery")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "realname", value = "姓名", paramType = "query"),
            @ApiImplicitParam(name = "clazzId", value = "杰普班级编号", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", paramType = "query"),
            @ApiImplicitParam(name = "channelIds", value = "渠道", paramType = "query"),
            @ApiImplicitParam(name = "stuTypes", value = "学生类型", paramType = "query"),
            @ApiImplicitParam(name = "year", value = "毕业年份", paramType = "query"),
    })
    public Message pageQuery(@NotNull int page, int pageSize, String realname,
                             Long clazzId, String status, Long[] channelIds, String[] stuTypes, String year) {
        System.out.println("【————————pageQueryStudent————————】");
        System.out.println(page);
        System.out.println(pageSize);
        System.out.println(realname);
        System.out.println(clazzId);
        System.out.println(status);
        System.out.println(Arrays.toString(channelIds));
        System.out.println(Arrays.toString(stuTypes));
        System.out.println(year);
        System.out.println("【————————pageQueryStudent end————————】");
        PageVM<StudentExtend> pageVM = studentPrimaryService.pageQuery(page, pageSize,realname,clazzId,status,channelIds,stuTypes,year);
        return MessageUtil.success(pageVM);
    }

    // 下载学生信息
    @ApiOperation(value = "导入到Excel中", notes = "注意！测试的时候请将地址粘贴到浏览器地址栏测试", produces = "application/octet-stream")
    @GetMapping("download")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "realname", value = "姓名", paramType = "query"),
            @ApiImplicitParam(name = "clazzId", value = "杰普班级编号", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", paramType = "query"),
            @ApiImplicitParam(name = "channelIds", value = "渠道", paramType = "query"),
            @ApiImplicitParam(name = "stuTypes", value = "学生类型", paramType = "query"),
            @ApiImplicitParam(name = "year", value = "毕业年份", paramType = "query"),
    })
    public void download(HttpServletResponse response, @NotNull int page, int pageSize, String realname, Long clazzId,
                         String status, Long[] channelIds, String[] stuTypes, String year) throws Exception {
        // 2. 将excel导出
        // 2.1 excel头部数据
        String[] headList = new String[] { "姓名","杰普班级","状态","学校","学院","专业","校内班级","校内学号","性别", "民族","出生日期", "学历","大学毕业时间","外语能力","是否关注","备注" };
        // 2.2 属性
        String[] fieldList = new String[] { "realname", "clazz","status","school","college","major","schoolStuClazz","schoolStuNo","gender", "nation","birth","education","graduationTime","flp","isAttention","comment"};
        // 2.3 数据列，需要转化
        List<Map<String, Object>> dataList = new ArrayList<>();
        PageVM<StudentExtend> pageVm = studentService.pageQuery(page, pageSize,realname,clazzId,status,channelIds,stuTypes,year);

        // 日期处理函数
        SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
        //ddTel  "idCard","tel","qq","guardianTel" ,
        for (StudentExtend u : pageVm.getList()) {
            Map<String, Object> map = new HashMap<>();
            map.put("realname", u.getRealname());
            map.put("clazz",u.getClazz());
            map.put("status",u.getStatus());
            // 毕业院校
            if(u.getChannel()!=null) {
                // 学院
                map.put("college", u.getChannel().getName());
                if(u.getChannel().getParent()!=null) {
                    // 学校
                    map.put("school", u.getChannel().getParent().getName());
                }
            }
            map.put("major", u.getMajor());
            map.put("schoolStuClazz", u.getSchoolStuClazz());
            map.put("schoolStuNo", u.getSchoolStuNo());
            map.put("gender", u.getGender());
            map.put("nation",u.getNation());
            // 出生日期
            String birth = "";
            try {
                birth = formatter.format(u.getBirth());
            } catch(Exception e) {
                birth = u.getBirth()+"";
            }
            map.put("birth", birth);
            // 毕业时间
            String  graduationTime ="";
            try	{
                graduationTime = formatter.format(u.getGraduationTime());
            } catch (Exception e){
                graduationTime = u.getGraduationTime()+"";
            }
            map.put("graduationTime",graduationTime);
            map.put("education", u.getEducation());
            map.put("flp", u.getFlp());
            map.put("comment", u.getComment());
            map.put("isAttention", u.getIsAttention());
            dataList.add(map);
        }
        // 2.4 导出文件名
        String excelName = "student_list";
        // 3. 调用工具方法导出Excel
        ExcelUtils.createExcel(response, excelName, headList, fieldList, dataList);
    }
}
